package cn.kais.drawer.bean;

import cn.kais.drawer.AxisUtil;
import cn.kais.drawer.UnitUtil;
import cn.kais.drawer.menu.MenuDatePattern;
import cn.kais.drawer.menu.MenuTextEven;
import cn.kais.drawer.menu.MenuTimePattern;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.awt.*;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import java.util.Date;

public class BeanTime extends BeanBasic {

    /**
     * 入参 时间串 必须格式：yyyy-MM-dd HH:mm:ss
     */
    public String text = "2024-11-25 18:36:59";

    public int top;
    public int left;
    public int width;
    public int height;

    public Font font;   // 文本字体
    public Color color; // 文本颜色

    public int fontSize;    // 字体大小
    public String fontName; // 字体名称
    public boolean isBold;  // 是否加粗
    public boolean isItalic;// 是否斜体

    public String dateSeparator = "/";
    public MenuDatePattern dateFormat = MenuDatePattern.YMD;  // 格式化

    public String timeSeparator;
    public MenuTimePattern timeFormat;

    public String preText;
    public String sufText;

    public String textAlign;
    public String verticalAlign;

//    public boolean isZoom = true;// 是否缩放

    /**
     * 文本对齐方式
     * 左右对齐 可以和 上下对齐/垂直居中 合并使用
     * 上下对齐 可以和 左右对齐/水平居中 合并使用
     * 水平居中 可以和 上下对齐/垂直居中 合并使用
     * 垂直居中 可以和 左右对齐/水平居中 合并使用
     */
    // 左中右
    public MenuTextEven even1 = MenuTextEven.CH;
    // 上中下
    public MenuTextEven even2 = MenuTextEven.CV;

    public Graphics2D sd; // 绘制

    public BeanTime(Graphics2D sd, JSONObject com) {
        this.sd = sd;
        this.top = com.getInteger("top");
        this.left = com.getInteger("left");
        this.width = com.getInteger("width");
        this.height = com.getInteger("height");

//        this.time = com.getString("text");
        this.text = UnitUtil.doGetNowDate();
        this.dateSeparator = com.getString("dateSeparator");
        this.dateFormat = MenuDatePattern.onParseCode(com.getString("dateFormat"));

        this.timeSeparator = com.getString("timeSeparator");
        this.timeFormat = MenuTimePattern.onParseCode(com.getString("timeFormat"));

        this.preText = com.getString("preText");
        this.sufText = com.getString("sufText");

        this.fontName = com.getString("font");
        this.fontSize = com.getInteger("fontSize");
        this.isBold = com.getBoolean("isBold");
        this.isItalic = com.getBoolean("isItaly");
        this.font = UnitUtil.toFont(this.fontName, this.fontSize, this.isBold, this.isItalic);
        this.color = UnitUtil.toColor(com.getString("fontColor"));

        this.textAlign = com.getString("textAlign");
        this.verticalAlign = com.getString("verticalAlign");
        even1 = MenuTextEven.onParseCode("H-" + this.textAlign);
        even2 = MenuTextEven.onParseCode("V-" + this.verticalAlign);

        this.create();
    }

    private void create() {
        long start = System.currentTimeMillis();
        String value = "";
        if (StringUtils.isNotBlank(this.preText)) {
            value = this.preText;
        }

        // 各种转换后 实际绘制的文本
        Date date = UnitUtil.doStr2Date(this.text); // 字符串转 date 类型
        String strDate = UnitUtil.doDate2Str(date, this.dateFormat);
        String strDateText = UnitUtil.doTakeTurnsTime(strDate, this.dateSeparator);
        System.out.println(strDate);
        System.out.println(strDateText);
        value = value + strDateText;

        if (this.timeFormat != MenuTimePattern.NONE) {
            String strTime = UnitUtil.doDate2Str(date, this.timeFormat);
            String strTimeText = UnitUtil.doTakeTurnsTime(strTime, this.timeSeparator);
            System.out.println(strTime);
            System.out.println(strTimeText);
            value = value + strTimeText;
        }

        if (StringUtils.isNotBlank(this.sufText)) {
            value = value + this.sufText;
        }

        System.out.println(value);

//        text = "中文汉字需要自适应宽度ABC,1";
//        if (isZoom) { // 宽度自适应
//            FontMetrics metrics = this.sd.getFontMetrics(this.font);
//            int w = metrics.stringWidth(text);
//            if (w > this.width) {
//                // 自适应 需要重新计算文本fontsize
//                int size = this.doCountTextSize(text);
//                this.fontSize = this.width / size;
//                this.font = UnitUtil.toFont(this.fontName, this.fontSize, this.isBold, this.isItalic);
//            }
//        }

        AttributedString as = new AttributedString(value);
        as.addAttribute(TextAttribute.FONT, this.font);
        as.addAttribute(TextAttribute.FOREGROUND, this.color);

        FontMetrics metrics = this.sd.getFontMetrics(this.font);

//        System.out.println(metrics.getHeight());
//        System.out.println(metrics.getAscent() + ":" + metrics.getMaxAscent());
//        System.out.println(metrics.getDescent() + ":" + metrics.getMaxDescent());
//        System.out.println(metrics.getLeading());

        int fontHeight = metrics.getAscent() - metrics.getDescent() - metrics.getLeading();
        int fontWidth = metrics.stringWidth(value);
        System.out.println("字体高度：" + fontHeight);
        System.out.println("字体宽度：" + fontWidth);

        int x = AxisUtil.doCountTextEvenX(this.even1, this.left, this.width, fontWidth);
        int y = AxisUtil.doCountTextEvenY(this.even2, this.top, this.height, fontHeight);
        sd.drawString(as.getIterator(), x, y);

        // 区域框
        if (this.isShowBorder) {
            new BeanBorder(this.sd, this.top, this.left, this.width, this.height);
        }

        long end = System.currentTimeMillis();
        System.out.println("绘制时间字符串时长：" + (end - start));
    }


    /**
     * 缩放使用
     * 计算文本实际长度 （一个中文 1个长度 2个英文或字符1个长度）
     * 向上取整 0.5 也算一个长度
     */
    private int doCountTextSize(String text) {
        double c1 = 0;
        double c2 = 0;
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (isChinese(c)) {
                c1 = c1 + 1;
            } else {
                c2 = c2 + 1;
            }
        }
        double cc = c1 + (c2 / 2);
        return (int) Math.round(cc);
    }

    /**
     * 判断字符是否为中文
     *
     * @param c 字符
     * @return 是否中文
     */
    private boolean isChinese(char c) {
        return c >= 0x4E00 && c <= 0x9FA5;
    }

}
